<!--主平台-->
<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <user-search @search="reload" :oemList="oemList" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="systemUserTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            新建
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
          <!--          <el-button-->
          <!--            size="small"-->
          <!--            icon="el-icon-upload2"-->
          <!--            class="ele-btn-icon"-->
          <!--            @click="openImport"-->
          <!--          >-->
          <!--            导入-->
          <!--          </el-button>-->
        </template>
        <!-- 用户名列 -->
        <template v-slot:nickname="{ row }">
          <router-link :to="'/system/user/details?id=' + row.userId">
            {{ row.nickname }}
          </router-link>
        </template>
        <!-- 角色列 -->
        <template v-slot:roles="{ row }">
          <el-tag
            v-for="item in row.roles"
            :key="item.roleId"
            size="mini"
            type="primary"
            :disable-transitions="true"
          >
            {{ item.roleName }}
          </el-tag>
        </template>
        <!--设备图片-->
        <template v-slot:equipmentPicture="{ row }">
          <div class="flex">
            <el-image :src="row.equipmentPicture"
                      :preview-src-list="[row.equipmentPicture]"style="height: 40px;"></el-image>
          </div>
        </template>
        <!-- 状态列 -->
        <template v-slot:equipmentStatus="{ row }">
          <el-tag
            size="mini"
            :disable-transitions="true"
            :type="['success', 'danger'][row.equipmentStatus]"
          >
            <!--{{ ['使用中', '闲置中'][row.equipmentStatus] }}-->
            {{row.equipmentStatus==1 ? '闲置中' : '使用中'}}
          </el-tag>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-refresh"
            style="color: #9932CC"
            @click="openTransfer(row)"
          >
            划拨
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除该设备吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑/修改弹窗 -->
    <user-edit :visible.sync="showEdit" :oemList="oemList" :data="current" @done="reload" />
    <!-- 划拨代理弹窗 -->
    <equipment-edit :visible.sync="showTransfer" :oemList="oemList" :data="current" @done="reload" />
    <!-- 导入弹窗 -->
    <user-import :visible.sync="showImport" @done="reload" />
  </div>
</template>

<script>
  import UserSearch from './components/user-search.vue';
  import UserEdit from './components/user-edit.vue';
  import UserImport from './components/user-import.vue';
  import EquipmentEdit from './components/equipment-edit.vue';
  import {
    pageMerchant,
    removeUser,
    removeUsers,
    updateUserStatus,
    updateUserPassword
  } from '@/api/equipmentManagement';
  import {oemAgentQuery} from '@/api/oem'
  import {JSEncrypt} from "jsencrypt";

  export default {
    name: 'SystemUser',
    components: {
      UserSearch,
      EquipmentEdit,
      UserEdit,
      UserImport
    },
    data() {
      return {
        // 查询全部代理列表
        oemList:[],
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: 'left'
          },
          {
            columnKey: 'index',
            type: 'index',
            label: '序号',
            width: 60,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'equipmentCode',
            label: '设备编号',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'equipmentCode'
          },
          {
            prop: 'equipmentName',
            label: '设备名称',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'agentName',
            label: '所属代理',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'equipmentPicture',
            label: '设备图片',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110,
            align: 'center',
            slot: 'equipmentPicture'
          },
          {
            columnKey: 'equipmentStatus',
            prop: 'equipmentStatus',
            label: '设备状态 ',
            showOverflowTooltip: true,
            slot: 'equipmentStatus',
            filterMultiple: false,
            align: 'center',
            /*filters: [
              {
                text: '使用中',
                value: 2
              },
              {
                text: '闲置中',
                value: 1
              }
            ]*/
          },
          {
            prop: 'startTime',
            label: '创建时间',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 220,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示导入弹窗
        showImport: false,
        //是否显示划拨编辑弹窗
        showTransfer: false,
      };
    },
    // 路由刷新
    activated(){
      this.queryOem()
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        if (!where.deleted) where.deleted = 0
        return pageMerchant({ ...where, ...order, page, limit });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 打开编辑弹窗 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },
      // 查询所有代理
      queryOem(){
        oemAgentQuery(
          {
            limit:10,
            page:1,
            types:1,
            oemType:1,
          }
        )
          .then((data) => {
            console.log("data===",data)
            this.oemList=data.list
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      /* 打开划拨编辑弹窗 */
      openTransfer(row) {
        this.current = row;
        this.showTransfer = true;
      },
      /* 打开导入弹窗 */
      openImport() {
        this.showImport = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        removeUser({ids:[row.id]})
          .then((msg) => {
            loading.close();
            this.$message.success('删除成功！');
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除该设备吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            removeUsers({ids: this.selection.map((d) => d.id)})
              .then((msg) => {
                loading.close();
                this.$message.success('删除成功！');
                this.reload();
              })
              .catch((e) => {
                loading.close();
                this.$message.error(e.message);
              });
          })
          .catch(() => {});
      },
    }
  };
</script>
